package com.chen.positioning.recharge.controller;

import com.chen.positioning.amountconfig.entity.AmountConfig;
import com.chen.positioning.recharge.service.RechargeRecordService;
import com.chen.positioning.util.CommonJsonReturnMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author halo
 * @create 2019-08-24 11:15
 */
@Controller
@RequestMapping("/rechargeRecord")
public class RechargeRecordController {

    private static final Logger logger = LoggerFactory.getLogger(RechargeRecordController.class);

    @Autowired
    RechargeRecordService rechargeService;
    @Autowired
    CommonJsonReturnMessage commonJsonReturnMessage;

    /**
     * 充值包月初始页面
     *
     * @return
     */

    @RequestMapping(value = "/recharge_monthly", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public ModelAndView phonePositioning(HttpSession session) {
        logger.info("已进入充值包月初始页面....");
        List<AmountConfig> amountConfigList = new ArrayList<AmountConfig>();
        ModelAndView modelAndView = new ModelAndView();//实例化一个View的ModelAndView对象
        try {
            amountConfigList = rechargeService.findAmountConfigInfo();
            logger.info("充值包月初始页面[" + amountConfigList.get(0).toString() + "]");
            session.setAttribute("amountConfig", amountConfigList.get(0));
            modelAndView.setViewName("onlineRecharge");//转发到games.html
            //封装数据，用于前端页面渲染，这里我返回的是一个List。当然Map，Set等数据结构也是可以的。
            modelAndView.addObject("rechargeConfig", amountConfigList.get(0));
            return modelAndView;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("recharge_monthly error." + e);
            return modelAndView;
        }
    }
    /*@RequestMapping(value = "/goRecharge", method = {RequestMethod.POST, RequestMethod.GET})
    public String goRecharge() {
        logger.info("goRecharge....");
        return "onlineRecharge";
    }*/

    /**
     * 充值业务类
     *
     * @param session
     * @param response
     * @param cost
     * @return
     */
    @RequestMapping(value = "/choose_recharge", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public String choose_recharge(HttpSession session, HttpServletResponse response, int cost) {
        logger.info("需要支付的金额是:" + cost);
        try {
            if (cost > 0) {

            } else {
                return commonJsonReturnMessage.failReturn("充值金额必须大于O", "充值金额必须大于O");
            }
            return commonJsonReturnMessage.successReturn("充值OK", "充值OK");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("充值出错了", e);
            return commonJsonReturnMessage.failReturn("充值fail", "充值fail");
        }

    }
}
